package com.ozveri.menu;

import android.app.Activity;
import android.graphics.Rect;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Interpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MenuDemoActivity extends Activity implements OnClickListener {

	private static final float GESTURE_THRESHOLD_DP = 16;
	ImageView food, mug, beer, cake;
	TextView textview1, textview2, textview3, textview4, textview5;
	AnimationSet animationFood, animationBeer, animationMug, animationCake;
	RelativeLayout mainRelative;
	LinearLayout menuItems;
	private static final int DURATION = 2000;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		food = (ImageView) findViewById(R.id.imageView1);
		beer = (ImageView) findViewById(R.id.imageView2);
		mug = (ImageView) findViewById(R.id.imageView4);
		cake = (ImageView) findViewById(R.id.imageView3);

		textview1 = (TextView) findViewById(R.id.TextView01);
		textview2 = (TextView) findViewById(R.id.TextView02);
		textview3 = (TextView) findViewById(R.id.TextView03);
		textview4 = (TextView) findViewById(R.id.textView1);
		textview5 = (TextView) findViewById(R.id.textView2);
		mainRelative = (RelativeLayout)findViewById(R.id.MainRelative);
		menuItems = (LinearLayout) findViewById(R.id.menu_item_list);

		food.setOnClickListener(this);

	}

	public void onClick(View v) {

		System.out.println("can touch this");
		int[] locationFood = new int[2];
		food.getLocationOnScreen(locationFood);
		animationFood = new AnimationSet(false);
		TranslateAnimation animationFoodTranslate = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
				TranslateAnimation.ABSOLUTE, 50 - (locationFood[0]),
				TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE,
				10 - locationFood[1]);
		// animationFood.setInterpolator()
		ScaleAnimation animationFoodScale = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,ScaleAnimation.ABSOLUTE,0.5f,ScaleAnimation.ABSOLUTE, 0.5f);
		animationFood.addAnimation(animationFoodScale);
		animationFood.addAnimation(animationFoodTranslate);
		animationFood.setDuration(DURATION);
		animationFood.setAnimationListener(new foodAnimation());

		int[] locationBeer = new int[2];
		beer.getLocationOnScreen(locationBeer);
		
		animationBeer = new AnimationSet(false);
		TranslateAnimation animationBeerTranslate = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
				TranslateAnimation.ABSOLUTE, 50 - (locationBeer[0] ),
				TranslateAnimation.ABSOLUTE, 0, 
				TranslateAnimation.ABSOLUTE, 100 - locationBeer[1]);
		// animationFood.setInterpolator()
		
		
		ScaleAnimation animationBeerScale = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,ScaleAnimation.ABSOLUTE,0.5f,ScaleAnimation.ABSOLUTE, 0.5f);
		
		animationBeer.addAnimation(animationBeerScale);
		animationBeer.addAnimation(animationBeerTranslate);
		animationBeer.setDuration(DURATION);
		animationBeer.setAnimationListener(new beerAnimation());

		int[] locationCake = new int[2];
		cake.getLocationOnScreen(locationCake);
		animationCake = new AnimationSet(false);
		TranslateAnimation animationCakeTranslate = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
				TranslateAnimation.ABSOLUTE, 50 - (locationCake[0]),
				TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE,
				190 - locationCake[1]);
		// animationFood.setInterpolator()
		ScaleAnimation animationCakeScale = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,ScaleAnimation.ABSOLUTE,0.5f,ScaleAnimation.ABSOLUTE, 0.5f);
		animationCake.addAnimation(animationCakeScale);
		animationCake.addAnimation(animationCakeTranslate);
		animationCake.setDuration(DURATION);
		animationCake.setAnimationListener(new cakeAnimation());
		
		

		int[] locationMug = new int[2];
		mug.getLocationOnScreen(locationMug);
		animationMug = new AnimationSet(false);
		TranslateAnimation animationMugTranslate = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
				TranslateAnimation.ABSOLUTE, 50 - (locationMug[0]),
				TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE,
				280 - locationMug[1]);
		// animationFood.setInterpolator()
		ScaleAnimation animationMugScale = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,ScaleAnimation.ABSOLUTE,0.5f,ScaleAnimation.ABSOLUTE, 0.5f);
		
		animationMug.addAnimation(animationMugScale);
		animationMug.addAnimation(animationMugTranslate);
		animationMug.setDuration(DURATION);
		animationMug.setAnimationListener(new mugAnimation());

		food.startAnimation(animationFood);
		beer.startAnimation(animationBeer);
		mug.startAnimation(animationMug);
		cake.startAnimation(animationCake);
		

		
		

		textview1.setVisibility(TextView.GONE);
		textview2.setVisibility(TextView.GONE);
		textview3.setVisibility(TextView.GONE);
		textview4.setVisibility(TextView.GONE);
		textview5.setVisibility(TextView.GONE);
		
		for (int i = 0; i < 10; i++) {
			RelativeLayout inflateRow = (RelativeLayout) View.inflate(this, R.layout.main_table_row, null);
			menuItems.addView(inflateRow);
		}
		



	}

	public class mugAnimation implements AnimationListener {

		public void onAnimationEnd(Animation animation) {
			mug.clearAnimation();
			LayoutParams paramsMug = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			paramsMug.setMargins(50-mug.getWidth()/4, 280, 0, 0);
			paramsMug.height = mug.getHeight()/2;
			mug.setLayoutParams(paramsMug);

		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}

	}

	public class cakeAnimation implements AnimationListener {

		public void onAnimationEnd(Animation animation) {
			cake.clearAnimation();
			LayoutParams paramCake = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			paramCake.setMargins(50-cake.getWidth()/4, 190, 0, 0);
			paramCake.height = cake.getHeight()/ 2;
			cake.setLayoutParams(paramCake);
			
			
			RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(mainRelative.getLayoutParams());
			relativeParams.width = 200;
			mainRelative.setLayoutParams(relativeParams);

		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}

	}

	public class foodAnimation implements AnimationListener {

		public void onAnimationEnd(Animation animation) {
			food.clearAnimation();
			LayoutParams paramsFood = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			paramsFood.setMargins(50-food.getWidth()/4, 10, 0, 0);
			paramsFood.height = food.getHeight()/2;
			food.setLayoutParams(paramsFood);

		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}

	}

	// TODO needs to be loose class (gonna hurt though)
	public class beerAnimation implements AnimationListener {

		public void onAnimationEnd(Animation animation) {

			beer.clearAnimation();
			LayoutParams paramsBeer = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			paramsBeer.setMargins(50 - beer.getWidth()/4, 100, 0, 0);
			paramsBeer.height = beer.getHeight() / 2;
			beer.setLayoutParams(paramsBeer);

		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}

	}
}
